<template>
  <div>
      <h1> 欢迎 {{username}} 所持分值{{score}}</h1>
      <table>
          <tr>
              <th>课程</th>
              <th>人数</th>
              <th>价格</th>
              <th>操作</th>
          </tr>
          <tr v-for="course in course_list" :key="course.id">
              <td>{{course.name}}</td>
              <td>{{course.num}}</td>
              <td>{{course.price}}</td>
              <td><a @click="buy(course.id)">购买</a></td>
          </tr>
      </table>
  </div>
</template>

<script>
export default {
    data(){
        return {
            username:sessionStorage.getItem("username"),
            score:sessionStorage.getItem("score"),
            course_list:[]
        }
    },
    created(){
        this.axios.get("/course").then(resp=>{
            console.log(resp.data)
            this.course_list=resp.data
        })
    },
    methods:{
        buy(cid){
            this.axios.post("/cart",{"uid":sessionStorage.getItem("uid"),"cid":cid}).then(resp=>{
                console.log(resp.data)
                alert("购买成功")
                this.$router.go(0)
            })
        }
    }
}
</script>

<style>

</style>